package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQueryObject;
import cn.wolfcode.wolf2w.repository.DestinationMongoRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import com.sun.org.apache.xpath.internal.compiler.Keywords;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationMongoRepository repository;
    @Autowired
    private RegionServiceImpl regionService;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(Destination destination) {
        repository.save(destination);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public void update(Destination destination) {
        repository.save(destination);
    }

    @Override
    public Destination find(String id) {
        Destination destination = repository.findById(id).orElse(null);

        return destination;
    }

    @Override
    public List<Destination> findAll() {
        List<Destination> list = repository.findAll();
        return list;
    }

    @Override
    public List<Destination> getDestByRegionId(String rid) {
        Region region = regionService.find(rid);
        List<Destination> list = repository.findByIdIn(region.getRefIds());
        return list;
    }

    @Override
    public Page list(DestinationQueryObject qo) {
        Query query = new Query();

        if(!StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(null));
        }else{
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }
        if(StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }

        long count = template.count(query, Destination.class);
        if(count == 0){
            Page.empty();
        }
        Pageable page = PageRequest.of(qo.getCurrentPage() - 1,qo.getPageSize(),Sort.Direction.ASC,"_id");
        query.with(page);

        List<Destination> list = template.find(query, Destination.class);
        return new PageImpl(list,page,count);
    }

    @Override
    public List<Destination> getToasts(String parentId) {
        ArrayList<Destination> list = new ArrayList<>();
        if(StringUtils.hasLength(parentId)){
            //根据parentId查目的地
            Destination dest = this.find(parentId);
            list.add(dest);
            //当dest的partentId不为空时进入循环
            while(StringUtils.hasLength(dest.getParentId())){
                dest = this.find(dest.getParentId());
                list.add(dest);
            }
        }
        Collections.reverse(list);
        return list;
    }

    @Override
    public List<Destination> search(String regionId) {
        //根据refIds查目的地
        List<Destination> list = null;
        if("-1".equals(regionId)){
            list = repository.findByParentName("中国");
        }else{
            List<String> refIds = regionService.find(regionId).getRefIds();
            list = repository.findByIdIn(refIds);
        }

        for (Destination dest : list) {
            //给集合中children设置值
            PageRequest of = PageRequest.of(0, 5);
            List<Destination> childrens = repository.findByParentId(dest.getId(),of);
            dest.setChildren(childrens);
        }

        return list;
    }

    @Override
    public Destination getByName(String name) {
        Destination dest = repository.findByName(name);
        return dest;
    }

}
